<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018/8/29 0029
 * Time: 11:18
 */
namespace app\index\model;
use app\admin\model\JurisdictionModel;
use app\admin\model\SettingModel;
use think\Db;

class Api
{
    /**获取当前部门某月的上班天数
     * @param $departmen_id:获取上班天数的部门id
     * @param null $time:获取的年月 如2018-08
     * @return bool|int
     */
    static public function getWorkingDays($departmen_id,$time=null){
        $setting = SettingModel::getSetting()->where('card_setting.department_id',$departmen_id)->find();
        if($setting){
            $attendance = $setting['attendance'];
            $number = 0;
            if(!$time){
                $time = date("Y-m",time());
            }
            if($time==date("Y-m",time())){
                $days = date('d',time());
            }else{
                $days = date('t',strtotime($time));
            }
                for ($a=1;$a<=$days;$a++){
                    $str = $a;
                    if($a<10){
                        $str = '0'.$a;
                    }
                    $str = $time.'-'.$str;
                    $w = date('w',strtotime($str));
                    if(in_array($w,$attendance)){
                        $number++;
                    }
                }
//                if(($time==date("Y-m",time()))&&(in_array(date('w',time()),$attendance))){
//                    $number--;
//                }
                return $number;

        }else{
            return false;
        }
    }

    /**获取某部门上班的日期
     * @param $departmen_id:部门id
     * @param null $time:时间
     * @return array|bool
     */
static public function getDays($departmen_id,$time=null){
    $setting = SettingModel::getSetting()->where('card_setting.department_id',$departmen_id)->find();
    if($setting){
        if(!$time){
            $time = time();
        }
        $attendance = $setting['attendance'];
        $number = [];
        if(!$time){
            $time = date("Y-m",time());
        }
        if($time==date("Y-m",time())){
            $days = date('d',time());
        }else{
            $days = date('t',strtotime($time));
        }
        for ($a=1;$a<=$days;$a++){
            $str = $a;
            if($a<10){
                $str = '0'.$a;
            }
            $str = $time.'-'.$str;
            $w = date('w',strtotime($str));
            if(in_array($w,$attendance)){
                $number[] = $a;
            }
        }
        return $number;
    }else{
        return false;
    }
}
    /**检测权限是否足够
     * @param $auth:权限名称
     * @param null $departmen_name:需要的部门
     * @return bool
     */
    static function AuthIndex($auth,$departmen_name=null){
        $user = session('user');
        if(!$user){
            return false;
        }
        if($departmen_name){
            if($user['departmen_name']!==$departmen_name){
                return false;
            }
        }
        //如果输入的是一个权限名称
        if(((int)$auth)==0){
            $jurisdiction = JurisdictionModel::getJurisdiction()
                ->where('jurisdiction_name',$auth)->find();
            if($user['jurisdiction']>=$jurisdiction['amounts']){
                return true;
            }else{
                return false;
            }
        }else{
            //如果输入的是权限id
            $jurisdiction = JurisdictionModel::getJurisdiction()
                ->where('id',$auth)->find();
            if($user['jurisdiction']>=$jurisdiction['amounts']){
                return true;
            }else{
                return false;
            }
        }
    }

    /**获取某段时间内打卡的时间戳
     * @param $start:开始时间戳
     * @param $end:结束时间戳
     * @param $departmen_id:所在部门的id
     * @return array
     */
    static public function getLeaveTime($start,$end,$departmen_id){
        $setting = SettingModel::getSetting()
            ->where('card_setting.department_id',$departmen_id)
            ->find();
        $tianshu = ($end-$start)/86400;
        $data = [];
        for ($a = 0;$a<$tianshu;$a++){
            $time = $start+$setting['work_shift']+$a*86400;
            if(in_array(date('w',$time),$setting['attendance'])){
                $data['toWork'][] = $time;
            }
            $timex = $start+$setting['closing_time']+$a*86400;
            if(in_array(date('w',$timex),$setting['attendance'])){
                $data['offWork'][] = $timex;
            }
        }
        return $data;
    }

    /**获取某人的未读信息条数
     * @return bool|float|string
     */
    static public function getNews(){
        if(!$user=isSign()){
            return false;
        }
        $data = Db::name('news')
            ->where('receive_id',$user['id'])
            ->where('status',0)
            ->count();
        return $data;
    }
    public function getne(){
        if(!$user=isSign()){
            return false;
        }
        $data = Db::name('news')
            ->where('receive_id',$user['id'])
            ->where('status',0)
            ->count();
        return $data;
    }
}